<template>
  <div>
    <h2 class="warning-text">在当前节点完成安装前，相关DNS解析记录将不会生效。</h2>

    <el-card class="install-method">
      <div slot="header" class="clearfix">
        <span>方法1：通过SSH自动安装</span>
      </div>
      <div class="ssh-address">
        <div slot="header" class="clearfix">
          <span>SSH地址</span>
          <p class="help-text">{{ nodeSshAddress }}</p>
        </div>
        <p>安装</p>
        <el-button type="primary" @click="startSSHInstall">开始安装</el-button>
      </div>
    </el-card>

    <el-card class="install-method">
      <div slot="header" class="clearfix">
        <span>方法2：手动安装</span>
      </div>
      <div>
        <span class="help-text">下载安装文件</span>
        <p>文件名：<span class="filename">cdn-modules-nodes-client.jar</span></p>
        <p>安装目录：{{installDir}}</p>
        <el-button  @click="markAsInstalled">修改当前节点为已安装状态</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {getNodeSshAndPort, installNode} from "@/api/nodes/nodeInfo";
import {getInstallPath} from "@/api/nodes/clusters";

export default {
  data() {
    return {
      sshAddress: '',
      installDirectory: '',
      nodeInfo:{},
      nodeSshAddress:'',
      installDir:'',
      port:null
    };
  },
  created() {
    this.nodeInfo = this.$route.params.nodeInfo
    this.getNodeSshAndPort();
    this.getInstallPath();
  },
  methods: {
    // 获取集群安装目录
    getInstallPath(){
      getInstallPath(this.nodeInfo.nodeId).then(response => {
        this.installDir = response
      })
    },
    // 获取节点的ssh地址
    getNodeSshAndPort(){
      getNodeSshAndPort(this.nodeInfo.nodeId).then(repose=>{
        this.sshAddress = repose.data.ipAddress
        this.port = repose.data.port
        this.nodeSshAddress = repose.data.ipAddress + ':' + repose.data.port
      })
    },
    startSSHInstall() {

      const data = {
        // 节点Id
        nodeId: this.nodeInfo.nodeId,
        // 节点主机地址
        ipAddress: this.sshAddress,
        // 节点主机端口
        port: this.port,
        // 节点安装目录
        installDir: this.installDir
      }
      console.log(data,'data')
      // 安装节点
      installNode(data).then(response=>{
        console.log(response,'response')
      })
    },
    markAsInstalled() {
      // 修改节点安装状态为已安装
      console.log('节点已标记为已安装');
    }
  }
};
</script>

<style scoped lang="scss">
.warning-text {
  color: red;
  margin-bottom: 10px;
  font-size: 14px;
  margin-left: 14px;
}

.install-method {
  margin-bottom: 20px;
  .filename {
    font-family: monospace;
  }
}

.ssh-address {
  margin-bottom: 20px;
  .filename {
    font-family: monospace;
  }
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.help-text {
  font-size: 12px;
  color: #888;
  margin-top: 5px;
  display: block;
}
</style>
